<template>
  <div class="app">
    <div class="box">
      <div class="circle-box">
        <div class="circle"></div>
        <div class="circle"></div>
      </div>
      <div class="bg-filter">
        <ul class="bubble-bgwall">
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
        </ul>

        <div class="login-wrap">
          <el-row>
            <el-col :xs="2" :sm="4" :md="6" :lg="8" :xl="9">
              <div class="grid-content"></div>
            </el-col>
            <el-col :xs="20" :sm="16" :md="12" :lg="8" :xl="6"
              ><el-form
                ref="form"
                :model="loginParams"
                label-width="12px"
                label-position="right"
                class="form login-container"
              >
                <el-form-item class="login-title">
                  <div>
                    <h1>欢迎登录</h1>
                  </div>
                </el-form-item>
                <el-form-item class="loginMethod">
                  <center>
                    <div>
                      <div class="search_class_type">
                        <div
                          :class="{ class_avtive: isPassword === true }"
                          @click="set_password"
                        >
                          密码登录
                        </div>
                        <div
                          :class="{ class_avtive: isCode === true }"
                          @click="set_code"
                        >
                          验证码登录
                        </div>
                      </div>
                    </div>
                  </center>
                </el-form-item>
                <div v-show="isPassword">
                  <el-form-item
                    label="*"
                    required
                    :rules="[
                      {
                        required: true,
                        message: '请输入电子邮箱',
                        trigger: 'blur',
                      },
                    ]"
                    prop="email"
                  >
                    <el-input
                      v-model="loginParams.email"
                      placeholder="请输入电子邮箱"
                      clearable
                      @keyup.enter.native="login('')"
                      ><i slot="prefix" class="el-icon-user"></i> </el-input
                  ></el-form-item>
                  <el-form-item
                    label="*"
                    :rules="[
                      {
                        required: isPassword,
                        message: '请输入密码',
                        trigger: 'blur',
                      },
                    ]"
                    prop="password"
                    ><el-input
                      placeholder="请输入密码"
                      v-model="loginParams.password"
                      show-password
                      clearable
                      @keyup.enter.native="login('')"
                      ><i slot="prefix" class="el-icon-lock"></i></el-input
                  ></el-form-item>
                </div>
                <div v-show="isCode">
                  <el-form-item
                    label="*"
                    required
                    :rules="[
                      {
                        required: true,
                        message: '请输入电子邮箱',
                        trigger: 'blur',
                      },
                    ]"
                    prop="email"
                  >
                    <el-input
                      v-model="loginParams.email"
                      placeholder="请输入电子邮箱"
                      clearable
                      @keyup.enter.native="login('')"
                      ><i slot="prefix" class="el-icon-user"></i> </el-input
                  ></el-form-item>
                  <el-row style="display: flex">
                    <el-col :span="15">
                      <el-form-item
                        prop="code"
                        style="width: 100%"
                        label="*"
                        :rules="[
                          {
                            required: isCode,
                            message: '请输入验证码',
                            trigger: 'blur',
                          },
                        ]"
                      >
                        <el-input
                          style="width: 100%"
                          type="text"
                          v-model="loginParams.code"
                          placeholder="请输入验证码"
                          autocomplete="off"
                          @keyup.enter.native="confirm"
                        ></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="9">
                      <el-form-item>
                        <el-button
                          @click="send"
                          :disabled="getCodeBtnDisable"
                          :class="{ 'disabled-style': getCodeBtnDisable }"
                          style="width: 100%; cursor: pointer; margin-top: -4%"
                          class="code"
                          >{{ codeBtnWord }}</el-button
                        >
                      </el-form-item>
                    </el-col>
                  </el-row>
                </div>
                <el-form-item size="mini">
                  <el-checkbox v-model="isRemember" style=""
                    >记住我</el-checkbox
                  >
                  <el-link
                    type="primary"
                    style="float: right"
                    @click="$router.push('/login/forgotPassword')"
                    >忘记密码?</el-link
                  >
                </el-form-item>
                <el-form-item
                  ><el-button
                    @click="login"
                    :loading="loginLoading"
                    round
                    style="width: 100%"
                    >{{ loginLoading ? "正在登录中..." : "登录" }}</el-button
                  >
                </el-form-item>
                <el-form-item>
                  <el-link
                    type="primary"
                    @click="register"
                    style="float: right; margin-top: 10%"
                    >立即注册></el-link
                  >
                </el-form-item>
              </el-form></el-col
            >
            <el-col :xs="2" :sm="4" :md="6" :lg="8" :xl="9">
              <div class="grid-content"></div>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as CommonUtil from "@/utils/commonUtil";
import { sendCode } from "../../api/sendCode";

export default {
  data() {
    return {
      codeBtnWord: "获取验证码", // 获取验证码按钮文字
      waitTime: 61, // 获取验证码按钮失效时间
      isPassword: true,
      isCode: false,
      // 登录表单参数
      loginParams: {
        email: "",
        code: "",
        password: "",
      },
      // 登录按钮加载控制
      loginLoading: false,
      isRemember: false, // 是否记住账户
    };
  },
  mounted() {},
  computed: {
    // 控制获取验证码按钮是否可点击
    getCodeBtnDisable: {
      get() {
        if (this.waitTime == 61) {
          if (this.loginParams.email) {
            return false;
          }
          return true;
        }
        return true;
      },
      // 注意：因为计算属性本身没有set方法，不支持在方法中进行修改，而下面我要进行这个操作，所以需要手动添加
      set() {},
    },
  },
  methods: {
    set_password() {
      this.isPassword = true;
      this.isCode = false;
    },
    set_code() {
      this.isCode = true;
      this.isPassword = false;
      this.loginParams.password = "";
    },
    // 发送验证码
    send() {
      this.code_disabled = true;
      sendCode(this.loginParams.email)
        .then((res) => {
          console.log(res);
          if (res.data.code == 20000) {
            // this.$message.success("验证码发送成功!");
            this.$notify({
              title: "成功",
              message: "验证码在五分钟内容有效",
              type: "success",
            });
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((error) => {
          this.confirm_disabled = false;
          this.$message.error(error, "验证码发送失败，请稍后重试！");
        });
      // 因为下面用到了定时器，需要保存this指向
      let that = this;
      that.waitTime--;
      that.getCodeBtnDisable = true;
      this.codeBtnWord = `${this.waitTime}s 后重新获取`;
      let timer = setInterval(function () {
        if (that.waitTime > 1) {
          that.waitTime--;
          that.codeBtnWord = `${that.waitTime}s 后重新获取`;
        } else {
          clearInterval(timer);
          that.codeBtnWord = "获取验证码";
          that.getCodeBtnDisable = false;
          that.waitTime = 61;
        }
      }, 1000);
    },
    //跳转注册页面
    register() {
      this.$router.push("/register");
    },
    /**
     * 登录事件
     * 1.调用登录接口获得token
     * 2.调用用户信息接口存入本地vuex
     */
    login() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loginLoading = true;
          console.log(this.loginParams);
          this.$store
            .dispatch("login", {
              params: this.loginParams,
            })
            .then(() => {
              //是否记住用户
              if (this.isRemember) {
                let user = {
                  email: this.loginParams.email,
                  password: this.loginParams.password,
                  code: this.loginParams.code,
                };
                CommonUtil.setStore("rememberUserInfo", user);
              } else {
                CommonUtil.removeStore("rememberUserInfo");
              }
              this.$store.commit("setIsLogin", true);
              //获取个人信息
              this.$store.dispatch("queryUser");
              this.loginLoading = false;
              // window.location.href = "/";
              this.$router.push("/");
              this.$message.success("登录成功");
            })
            .catch((res) => {
              this.loginLoading = false;
              this.$message.error(res.message);
              this.$notify({
                title: "登录失败提示",
                message:
                  "请前往注册或确保账号输入正确完整邮箱号，如：172110166@qq.com",
                type: "warning",
                duration: 0,
              });
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.center {
  text-align: center;
}

.grid-content {
  /* border-radius: 4px; */
  min-height: 36px;
}

.flex-warp {
  flex-wrap: wrap;
}

.login-title {
  text-align: center;
  /* border: 1px solid black; */
  margin-top: 5%;
  margin-bottom: 5%;
}

.login-title img {
  height: 32px;
  vertical-align: middle;
}

.login-title h1 {
  font-size: 20px;
  font-weight: 600;
  /* vertical-align: middle; */
  display: inline-block;
  line-height: 32px;
}

.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 5%;

  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 0px 35px 0px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.app {
  width: 100%;
  height: calc(100vh - 57px);
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(270deg, #f1e1ff, #b8ccff);
}

.box {
  width: 100%;
  height: 100%;
  position: relative;
}

.circle-box {
  width: 100%;
  height: 100%;
  border-radius: 10px;
  position: absolute;
  overflow: hidden;
}

.circle:first-child {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  border: 30px solid #afe7ff;
  box-sizing: border-box;
  position: absolute;
  top: -60px;
  left: -70px;
  animation: move-y 3.5s linear infinite;
}

.circle:last-child {
  width: 150px;
  height: 160px;
  border-radius: 50%;
  background: linear-gradient(136deg, #e6ecff 0%, #a4b4fc 100%);
  box-sizing: border-box;
  position: absolute;
  bottom: -30px;
  right: -30px;
  animation: move-y 5s ease-in-out infinite;
}

.bg-filter {
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.05);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(8px);
  border-radius: 10px;
  box-sizing: border-box;
  position: absolute;
}

@keyframes move-y {
  0% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-16px);
  }

  100% {
    transform: translateY(0);
  }
}

.bubble-bgwall {
  overflow: hidden;
  position: fixed;
  margin: 0 auto;
  width: 100%;
  height: 100%;
  padding: 0;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  animation: bubble 15s infinite;
  z-index: 999;
}

.bubble-bgwall li:nth-child(1) {
  left: 10%;
}

.bubble-bgwall li:nth-child(2) {
  left: 20%;
  width: 90px;
  height: 90px;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(3) {
  left: 25%;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  animation-duration: 8s;
}

.bubble-bgwall li:nth-child(5) {
  left: 70%;
}

.bubble-bgwall li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 255, 255, 0.2);
  animation-delay: 3s;
}

.bubble-bgwall li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(8) {
  left: 55%;
  width: 40px;
  height: 40px;
  font-size: 12px;
  animation-duration: 15s;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(9) {
  left: 25%;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(10) {
  left: 85%;
  width: 40px;
  height: 40px;
  animation-delay: 5s;
}

@keyframes bubble {
  0% {
    opacity: 0.5;
    transform: translateY(0) rotate(45deg);
  }

  25% {
    opacity: 0.75;
    transform: translateY(-400px) rotate(90deg);
  }

  50% {
    opacity: 1;
    transform: translateY(-600px) rotate(135deg);
  }

  100% {
    opacity: 0;
    transform: translateY(-1000px) rotate(180deg);
  }
}

.class_avtive {
  color: rgb(70, 100, 180);
  border-bottom: 2px solid rgb(70, 100, 180);
  background: linear-gradient(rgba(133, 254, 254, 0.493), rgb(255, 255, 255));
  border-radius: 15px 15px 0px 0px;
}

.search_class_type {
  justify-content: space-around;
}

.search_class_type div {
  display: inline-block;
  padding: 10px 40px 3px 40px;
  margin: 20px 0 5px 0;
  /* margin: 0 30px;
  flex: 1;
  text-align: center; */
  cursor: pointer;
}

.loginMethod {
  margin-top: -10%;
}
</style>
